<?php
include_once(dirname(__FILE__) . "/../../../config.php");
include_once(dirname(__FILE__) . "/../v1_base.php");

class task_controller extends v1_base {
    public function list_action() {
        $parser = get_request_assert("parser");
        $tasks = Task::allParser($parser);
        $data = $this->packArray($tasks);
        return json_encode(array("op" => "tasks", "data" => $data));
    }


    public function upload_action() {
        // logging::d("Debug", $_REQUEST, false);
        $arg = get_request_assert("arg");
        $title = $arg["title"];
        $comments = $arg["comments"];
        $pid = $arg["parser"];
        $parser = Parser::create($pid);
        if ($parser == null) {
            return $this->result(v1_base::kRet_NoSuchParser);
        }

        $dt = Date("Ym");
        $dir = UPLOAD_DIR . $parser->name() . "/$dt";

        logging::d("Debug", "upload file to $dir");
        $args = array(
            "parser" => $parser,
            "title" => $title,
            "comments" => $comments,
            "dt" => $dt,
        );

        $ret = Upload::uploadFileViaFileReader($dir, null, function($filename, $args) {
            $parser = $args["parser"];
            $dt = $args["dt"];
            $path = $parser->name() . "/$dt/$filename";
            $task = new Task();
            $task->setTitle($args["title"]);
            $task->setParser($parser->id());
            $task->setComments($args["comments"]);
            $task->setTarget($path);
            return $task->save();
        }, $args);

        $sock = new swoole_client(SWOOLE_SOCK_TCP);
        if ($sock->connect(APP_SERVER_IP, APP_SERVER_PORT, -1)) {
            $sync = array("op" => "sync", "data" => array());
            $sock->send(json_encode($sync));
            $sock->close();
        } else {
            logging::d("Debug", "connect failed. Error: {$sock->errCode}");
        }

        return $this->checkRet($ret);
    }

    public function result_action() {
        $taskid = get_request_assert("task");
        $post = get_request("post", null);
        $entry = get_request("entry", null);

        $task = Task::create($taskid);
        $pid = $task->parser();
        $parser = Parser::create($pid);
        if ($parser == null) {
            return $this->result(v1_base::kRet_NoSuchParser);
        }

        $dt = Date("Ym");
        $dir = RESULT_DIR . $parser->name() . "/$dt";

        $args = array(
            "parser" => $parser,
            "task" => $task,
            "dt" => $dt,
            "post" => $post,
            "entry" => $entry,
        );

        $ret = Upload::uploadFileViaFileReader($dir, null, function($filename, $args) {
            $parser = $args["parser"];
            $task = $args["task"];
            $dt = $args["dt"];
            $post = $args["post"];
            $entry = $args["entry"];

            if ($post == "unzip") {
                $ext = substr(strrchr($filename, '.'), 1);
                $pfn = basename($filename, ".$ext");

                $thefile = RESULT_DIR . $parser->name() . "/$dt/$filename";
                $thepath = RESULT_DIR . $parser->name() . "/$dt/$pfn";
                logging::d("Debug", "the path = " . $thepath);

                $command = "unzip \"$thefile\" -d \"$thepath\"";
                logging::d("Unzip", $command);
                $fp = popen($command, "r");
                while (!feof($fp)) {
                    $s = fgets($fp);
                    logging::d("Unzip", $s);
                }
                pclose($fp);
                $result = $parser->name() . "/$dt/$pfn/$entry";
            } else {
                $result = $parser->name() . "/$dt/$filename";
            }
            Task::updateResult($task->id(), $result);
            return array("op" => "result", "data" => array("id" => $task->id(), "result" => $result));
        }, $args);
        return $ret;
    }

    public function fail_action() {
        $taskid = get_request_assert("task");
        $ret = Task::updateError($taskid);
        return $this->checkRet($ret);
    }
}













